package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.main.ui.MainActivity;

import java.util.List;

public class UpdateOrderViewByVoice extends CreateOrderViewByVoice {

    public UpdateOrderViewByVoice(Context context) {
        super(context);
    }

    public UpdateOrderViewByVoice(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    //todo 订单修改商品信息
    public void updateGoodForVoice(MainActivity activity, GoodBean goodBean) {
        reset();
        mGoodBean = goodBean;
        mTempGoodId = goodBean.getTempId();
        handleRightWeightView(); //称重和累加称重，非重量单位不显示
        handleShowGoodPriceView();//是否显示商品价格和 获取商品价格方法都在里面
        getOrderRecPrice();//获取商品价格
        mainActivity = activity;
        mGoodName = mGoodBean.getName();
        goodNameTv.setText(mGoodName);
        if (mGoodBean.getGoodNum() > 0) {
            numberInputEd.setText(NumberUtils.subZeroAndDot(mGoodBean.getGoodNum() + ""));
        }
        if (mGoodBean.getPrice() >= 0) {
            priceInputEd.setText(NumberUtils.subZeroAndDot(mGoodBean.getPrice() + ""));
        }
        //累加称重
        if (goodBean.getAddWeightBeans()!=null){
            addWeightView.showView(goodBean.getAddWeightBeans());
        }
        if (mGoodBean.getId() > 0) { //已存在的商品
            goodUnitTv.setText(goodBean.getUnitName());
            mChoiceUnitTv.setVisibility(GONE);
            hidUpdateGoodNameImg();
            hidChoiceUnit();
        } else {
            showUpdateGoodNameImg();
            showUnitViewByApi();
            hidRightPrice();
        }
        handelInputEdFocus();

    }

    @Override
    public void updateView(List<GoodBean> goodBeans) {
      if (goodBeans==null || goodBeans.isEmpty()){
          mGoodBean.setId(0);
          goodNameTv.setText(mGoodName);
          showUnitViewByApi();
          return;
      }
      //这里通过名字+单位搜索，只可能查询到一个商品
      GoodBean searchGood=goodBeans.get(0);
      if (searchGood!=null){
          mChoiceUnitTv.setVisibility(GONE);
          goodUnitTv.setVisibility(VISIBLE);
          mGoodBean=searchGood;
          mGoodBean.setTempId(mTempGoodId);
          handleRightWeightView(); //称重和累加称重，非重量单位不显示
          handleShowGoodPriceView();//是否显示商品价格
          getOrderRecPrice();//获取商品价格
          goodNameTv.setText(mGoodBean.getName());
          if (!TextUtils.isEmpty(mGoodBean.getUnitName())) {
              goodUnitTv.setText(mGoodBean.getUnitName());
          }
          //todo 设置 数量和单价 哪个输入框获取焦点
          handelInputEdFocus();


      }
    }
}
